package com.syni.mdd.yhd.common.all.service.impl;

import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.dao.jpa.BmsPazarConditionDao;
import com.syni.mdd.yhd.common.all.entity.BmsAgent;
import com.syni.mdd.yhd.common.all.entity.BmsPazarCondition;
import com.syni.mdd.yhd.common.all.entity.BmsUserBusinessAuth;
import com.syni.mdd.yhd.common.all.service.BmsPazarConditionService;
import com.syni.mdd.yhd.common.all.utils.SpecificationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @className
 * @description TOO
 * @Author cfx
 * @DATE 2019/4/9 12:04
 * @VERSION 1.0
 **/
@Service
public class BmsPazarConditionImpl implements BmsPazarConditionService {
    @Autowired
    BmsPazarConditionDao bmsPazarConditionDao;

    @Override
    public BmsPazarConditionDao getBaseDao() {
        return bmsPazarConditionDao;
    }

    @Override
    public void addPazarCondition(BmsUserBusinessAuth bmsUserBusinessAuth, BmsAgent bmsAgent) {
        Date nowTime = new Date();
        Map<String,Object> conditionMap = new HashMap<>();
        conditionMap.put("businessId",bmsUserBusinessAuth.getBmsBusinessId());
        List<BmsPazarCondition> bmsPazarConditions = bmsPazarConditionDao.findAll(new SpecificationUtils(conditionMap));
        if(!(bmsPazarConditions != null && bmsPazarConditions.size() > 0)){
            if(bmsAgent != null) {
                BmsPazarCondition bmsPazarCondition = new BmsPazarCondition();
                bmsPazarCondition.setUserId(bmsUserBusinessAuth.getBmsUserId());
                bmsPazarCondition.setBusinessId(bmsUserBusinessAuth.getBmsBusinessId());
                bmsPazarCondition.setAgentId(bmsAgent.getAgentId());
                bmsPazarCondition.setNewTime(nowTime);
                bmsPazarCondition.setUpdateTime(nowTime);
                bmsPazarConditionDao.save(bmsPazarCondition);
            }
        }else {
            BmsPazarCondition bmsPazarCondition = bmsPazarConditions.get(0);
            bmsPazarCondition.setUserId(bmsUserBusinessAuth.getBmsUserId());
            bmsPazarConditionDao.save(bmsPazarCondition);
        }
    }

    @Override
    public void updatePazarConditionPayData(Integer businessId) {
        Map<String,Object> conditionMap = new HashMap<>();
        conditionMap.put("businessId",businessId);
        List<BmsPazarCondition> bmsPazarConditions = bmsPazarConditionDao.findAll(new SpecificationUtils(conditionMap));
        if(bmsPazarConditions != null && bmsPazarConditions.size() > 0){
            BmsPazarCondition bmsPazarCondition = bmsPazarConditions.get(0);
            bmsPazarCondition.setPayStatus(Constants.COMMON_FLAG_TRUE_INT);
            bmsPazarCondition.setUpdateTime(new Date());
            if(bmsPazarCondition.getPayTime() == null) {
                bmsPazarCondition.setPayTime(new Date());
            }
            bmsPazarConditionDao.save(bmsPazarCondition);
        }
    }

    @Override
    public void updatePazarConditionCouponData(Integer businessId) {
        Map<String,Object> conditionMap = new HashMap<>();
        conditionMap.put("businessId",businessId);
        List<BmsPazarCondition> bmsPazarConditions = bmsPazarConditionDao.findAll(new SpecificationUtils(conditionMap));
        if(bmsPazarConditions != null && bmsPazarConditions.size() > 0){
            BmsPazarCondition bmsPazarCondition = bmsPazarConditions.get(0);
            bmsPazarCondition.setCouponStatus(Constants.COMMON_FLAG_TRUE_INT);
            bmsPazarCondition.setUpdateTime(new Date());
            if(bmsPazarCondition.getCouponTime() == null) {
                bmsPazarCondition.setCouponTime(new Date());
            }
            bmsPazarConditionDao.save(bmsPazarCondition);
        }
    }
}
